<template>
  <div class="ratingselect">
    <div class="type">
      <span class="type-item positive" @click="select(2,$event)" :class="{'active' : selectType === 2}">{{ desc.all }}
        <span class="conut">{{ ratings.length }}</span>
      </span>
      <span class="type-item positive" @click="select(0,$event)" :class="{'active' : selectType === 0}">{{ desc.positive }}
        <span class="conut">{{ positives.length }}</span>
      </span>
      <span class="type-item negative" @click="select(1,$event)" :class="{'active' : selectType === 1}">{{ desc.negative }}
        <span class="conut">{{ negatives.length }}</span>
      </span>
    </div>
    <div class="switch" :class="{'on':onlyContent}" @click="toggleContent($event)">
      <span class="icon-check_circle"></span>
      <span class="text">只看有内容的评价</span>
    </div>
  </div>
</template>
<script>

  const POSITIVE = 0
  const NEGATIVE = 1
  const ALL = 2

  export default {
    props: {
      ratings: {
        type: Array,
        default() {
          return []
        }
      },
      selectType: {
        type: Number,
        default: ALL
      },
      onlyContent: {
        type: Boolean,
        default: false
      },
      desc: {
        type: Object,
        default() {
          return {
            all: '全部',
            positive: '满意',
            negative: '不满意'
          }
        }
      }
    },
    computed: {
      positives() {
        return this.ratings.filter((rating) => {
          return rating.rateType === POSITIVE
        })
      },
      negatives() {
        return this.ratings.filter((rating) => {
          return rating.rateType === NEGATIVE
        })
      }
    },
    methods: {
      select(type, event) {
        if(!event._constructed) {
          return
        }
        this.selectType = type
        this.$dispatch('ratingtype.select', type)
      },
      toggleContent(event) {
        if(!event._constructed) {
          return
        }
        this.onlyContent = !this.onlyContent
        this.$dispatch('content.toggle', this.onlyContent)
      }
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
  .ratingselect
    .type
      font-size: 0
      padding: 12px 0 18px 0
      border-bottom: 1px solid rgba(7, 17, 27, .1)
      .type-item
        display: inline-block
        width: 73px
        height: 38px
        line-height: 38px
        margin-right: 8px
        font-size: 12px
        text-align: center
        color: rgb(78, 85, 93)
        border-radius: 2px
        .conut
          font-size: 8px
        &.active
          color: #fff
        &.positive
          background-color: rgba(0, 160, 220, .2)
          &.active
            background-color: rgb(0, 160, 220)          
        &.negative
          background-color: rgba(77, 85, 93, .2)
          &.active
            background-color: rgb(77, 85, 93)
    .switch
      padding: 12px 0
      font-size: 12px
      line-height: 24px
      color: rgb(147, 153, 159)
      border-bottom: 1px solid rgba(7, 17, 27, .1)
      &.on
        .icon-check_circle
          color: #00c850
      .icon-check_circle
        vertical-align: top
        font-size: 24px
      .text
        vertical-align: top
        margin-left: 4px
        font-size: 16px
</style>